package com.greenant.store.activity;


import java.util.ArrayList;
import java.util.List;

import com.avos.avoscloud.AVUser;
import com.greenant.store.R;
import com.greenant.store.adapter.ExpressPagerAdapter;
import com.greenant.store.view.ExpressTabView1;
import com.greenant.store.view.ExpressTabView2;
import com.greenant.store.view.ExpressTabView3;
import com.greenant.store.view.ExpressTabView4;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.lsarah.utils.SystemBarTintManager;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/**
 * * 
 *
 * 
 * 
 */
public class ExpressManageActivity extends FragmentActivity implements OnRefreshListener<ListView> {
    private static final String TAG = "ExpressManageActivity";
    private ViewPager mPager;
    
    
    private Resources resources; 
	private ImageView iBack;
	private TextView tTitle;
	
	private Button mTabBtnLeft;
	private Button mTabBtnRight;
	
	
	private Button emrecv_tab_1;
	private Button emrecv_tab_2;
	private Button emrecv_tab_3;
	private Button emrecv_tab_4;
	
	
	ExpressTabView1 expressTabView1;
	ExpressTabView2 expressTabView2;
	ExpressTabView3 expressTabView3;
	ExpressTabView4 expressTabView4;
	
	private LinearLayout leftView;
	private LinearLayout rightView;
	
	
	List<View> views=new ArrayList<View>();;
	
	public static final int REQUESTCODE_TYPE_1=0;
	public static final String REQUEST_KEY_POSITION="REQUEST_KEY_POSITION";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SystemBarTintManager.setContentView(this, R.layout.activity_express_manage,
            getResources().getColor(R.color.firebrick));
        resources = getResources();
        
        
        final ActionBar actionBar = getActionBar();  
        if (actionBar!=null)
        {
	        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);  
	        actionBar.setCustomView(R.layout.actionbar);//�Զ���ActionBar����  
        
            tTitle = (TextView)findViewById(R.id.actionbar_title);
	        iBack = (ImageView)findViewById(R.id.left_btn);
	        
	        tTitle.setText(resources.getText(R.string.my_order));
	        
	        iBack.setOnClickListener(new OnClickListener()
	        {	
				@Override
				public void onClick(View v) {
	         	    finish();
				}
	        	
	        });	        
	        
        }
        InitWidth();        
        InitViewPager();
        InitTextView();
    }

    private void InitTextView()
    {

        
        mTabBtnLeft = (Button) this
				.findViewById(R.id.fragment_express_tab_btn_daiqu);
		mTabBtnRight = (Button) this
				.findViewById(R.id.fragment_express_tab_btn_yiqu);
		
		leftView= (LinearLayout) this
				.findViewById(R.id.leftView);
		rightView= (LinearLayout) this
				.findViewById(R.id.rightView);
		
		mTabBtnLeft.setSelected(true);
		mTabBtnRight.setSelected(false);
		mTabBtnLeft.setTextColor(getResources().getColor(R.color.white));
		mTabBtnRight.setTextColor(getResources().getColor(R.color.green));
		
		mTabBtnLeft.setOnClickListener(mOnClickListenerFotTabBtn);
		mTabBtnRight.setOnClickListener(mOnClickListenerFotTabBtn);
		
		emrecv_tab_1 = (Button) this
				.findViewById(R.id.emrecv_tab_1);
		emrecv_tab_2 = (Button) this
				.findViewById(R.id.emrecv_tab_2);
		emrecv_tab_3 = (Button) this
				.findViewById(R.id.emrecv_tab_3);
		emrecv_tab_4 = (Button) this
				.findViewById(R.id.emrecv_tab_4);
		emrecv_tab_1.setSelected(true);
		
		emrecv_tab_1.setOnClickListener(mOnTabLIstener);
		emrecv_tab_2.setOnClickListener(mOnTabLIstener);
		emrecv_tab_3.setOnClickListener(mOnTabLIstener);
		emrecv_tab_4.setOnClickListener(mOnTabLIstener);
		
    }

    private OnClickListener mOnClickListenerFotTabBtn = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if (v.equals(mTabBtnLeft)) {
				mTabBtnLeft.setSelected(true);
				mTabBtnRight.setSelected(false);
				mTabBtnLeft
						.setTextColor(getResources().getColor(R.color.white));
				mTabBtnRight.setTextColor(getResources()
						.getColor(R.color.green));
				
				leftView.setVisibility(View.VISIBLE);
				rightView.setVisibility(View.GONE);
			} else if (v.equals(mTabBtnRight)) {
				mTabBtnLeft.setSelected(false);
				mTabBtnRight.setSelected(true);
				mTabBtnLeft
						.setTextColor(getResources().getColor(R.color.green));
				mTabBtnRight.setTextColor(getResources()
						.getColor(R.color.white));
				
				leftView.setVisibility(View.GONE);
				rightView.setVisibility(View.VISIBLE);
			}
		}
	};
	
	private OnClickListener mOnTabLIstener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			emrecv_tab_1.setSelected(false);
			emrecv_tab_2.setSelected(false);
			emrecv_tab_3.setSelected(false);
			emrecv_tab_4.setSelected(false);
			if (v.equals(emrecv_tab_1)) {
				emrecv_tab_1.setSelected(true);
				mPager.setCurrentItem(0);
			} else if (v.equals(emrecv_tab_2)) {
				emrecv_tab_2.setSelected(true);
				mPager.setCurrentItem(1);
			} else if (v.equals(emrecv_tab_3)) {
				emrecv_tab_3.setSelected(true);
				mPager.setCurrentItem(2);
				expressTabView3.getFirstData();
			} else if (v.equals(emrecv_tab_4)) {
				emrecv_tab_4.setSelected(true);
				mPager.setCurrentItem(3);
				expressTabView4.getFirstData();
			}

		}};
	
    private void InitViewPager() {
        mPager = (ViewPager) findViewById(R.id.vPager);
       
        String userId="";
        
        AVUser user=   AVUser.getCurrentUser();
        if(user!=null)
        {
            userId=user.getObjectId();
        }
    	
    	expressTabView1 = (ExpressTabView1)LayoutInflater.from(this).inflate(R.layout.view_express_real_tab1, null);
    	expressTabView2 = (ExpressTabView2)LayoutInflater.from(this).inflate(R.layout.view_express_real_tab2, null);
    	expressTabView3=(ExpressTabView3) LayoutInflater.from(this).inflate(R.layout.view_express_real_tab3, null);
    	expressTabView4 =(ExpressTabView4) LayoutInflater.from(this).inflate(R.layout.view_express_real_tab4, null);
        
    	views.add(expressTabView1);
    	views.add(expressTabView2);
    	views.add(expressTabView3);
    	views.add(expressTabView4);
    	
    	
        mPager.setAdapter(new ExpressPagerAdapter(views));
        mPager.setCurrentItem(0);
        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				emrecv_tab_1.setSelected(false);
				emrecv_tab_2.setSelected(false);
				emrecv_tab_3.setSelected(false);
				emrecv_tab_4.setSelected(false);
				if (arg0==0) {
					emrecv_tab_1.setSelected(true);
				} else if (arg0==1) {
					emrecv_tab_2.setSelected(true);
				} else if (arg0==2) {
					emrecv_tab_3.setSelected(true);
					expressTabView3.getFirstData();
				} else if (arg0==3) {
					emrecv_tab_4.setSelected(true);
					expressTabView4.getFirstData();
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
    }

    private void InitWidth() {
    }

    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mPager.setCurrentItem(index);
        }
    };


	@Override
	public void onRefresh(PullToRefreshBase<ListView> arg0) {
		
	}
	public void goBack(View v)
	{
	    finish();
	}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode==Activity.RESULT_OK)
        {
           if(requestCode==REQUESTCODE_TYPE_1) 
           {
        	   if(data!=null)
        	   {
        		  String code= data.getStringExtra(CaptureActivity.KEY_CAPTURE_CODE);
        		  int position= data.getIntExtra(REQUEST_KEY_POSITION, 0);
        		  if(expressTabView1!=null)
        		   expressTabView1.modifyPositonData(position,code);
        	   }
               
           }
        }
    }
	
}
